package com.exam.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.exam.bean.PaperContentType;
import com.exam.service.PaperContentTypeService;
import com.exam.service.impl.PaperContentTypeServiceImpl;
import com.exam.util.Page;
import com.exam.util.PageResult;
import com.opensymphony.xwork2.ActionSupport;

public class SelectContentTypeAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private PaperContentType parentPaperContentType = new PaperContentType();
	private PaperContentType paperContentType = new PaperContentType();
	private List<PaperContentType> pctList = new ArrayList<PaperContentType>();
	private Page page = new Page();

	// 为分页处理服务的方法
	@SuppressWarnings("unchecked")
	public void listForContentTypePageService(PaperContentTypeService s,
			String strWhere) throws Exception {
		PageResult pageResult = null;
		getPage().setEveryPage(10);
		if (strWhere != null && !"".equals(strWhere)) {
			pageResult = s.findListByPage(strWhere, getPage());// 按条件查询
		} else {
			pageResult = s.findListByPage(getPage());// 简单分页
		}
		setPage(pageResult.getPage());// 获得分页信息
		setPctList(pageResult.getList());// 获得记录
	}

	//供最顶层按条件查询
	public String selectByWherePage(){
			PaperContentTypeService service = new PaperContentTypeServiceImpl();
			HttpServletRequest request = ServletActionContext.getRequest();
			String nameStr = request.getParameter("name");
			
			try {
				if(nameStr!=null&&!"".equals(nameStr)){
					if(page.getCurrentPage()>1){//存在条件的时候，将分页信息设置为初始态
						page = new Page();
					}
					listForContentTypePageService(service, "name='"+nameStr+"'");
				}else{
					listForContentTypePageService(service, "pid is null");
				}
			} catch (Exception e) {
				e.printStackTrace();
				return "exception";
			}
			return "selectByPage";
		}
		
		// 分页查询最顶层分类信息
		public String selectByPage() {
			PaperContentTypeService service = new PaperContentTypeServiceImpl();
			try {
				listForContentTypePageService(service, "pid is null");
			} catch (Exception e) {
				e.printStackTrace();
				return "exception";
			}
			return "selectByPage";
		}

		// 根据id查询分类信息
		public String selectById() {
			PaperContentTypeService service = new PaperContentTypeServiceImpl();
			HttpServletRequest request = ServletActionContext.getRequest();
			String strID = request.getParameter("id");
			if (strID != null) {
				try {
					setPaperContentType(service.findModel(Integer.parseInt(strID)));

					if (getPaperContentType().getPid() != null) {
						setParentPaperContentType(service
								.findModel(getPaperContentType().getPid()));
					}
				} catch (Exception e) {
					// e.printStackTrace();
					e.printStackTrace();
					return "exception";
				}
			}
			return "selectById";
		}
	
	public String select() {
		return selectByPage();
	} 

	public String selectWhere(){
		return selectByWherePage();
	}
	
	public PaperContentType getParentPaperContentType() {
		return parentPaperContentType;
	}

	public void setParentPaperContentType(
			PaperContentType parentPaperContentType) {
		this.parentPaperContentType = parentPaperContentType;
	}

	public PaperContentType getPaperContentType() {
		return paperContentType;
	}

	public void setPaperContentType(PaperContentType paperContentType) {
		this.paperContentType = paperContentType;
	}

	public List<PaperContentType> getPctList() {
		return pctList;
	}

	public void setPctList(List<PaperContentType> pctList) {
		this.pctList = pctList;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
}
